package ZaznaczObszar;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;


public class A001_WskazObszar implements tm_WskazObszar_interf {
	
	Rectangle rectangle;
	InfoPanel infopanel;

	
	//public JDialog frame;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		Rectangle rectangle = new Rectangle(0,0,0,0);
		A001_WskazObszar test = new A001_WskazObszar(rectangle);
		test.Obszar();
		
		System.out.println(rectangle.toString());

	
	}
	
	public A001_WskazObszar(Rectangle rectangle) {
		this.rectangle = rectangle;
	}

/*	*//**
	 * Create the frame.
	 * @param rectangle 
	 *//*
	public A001_WskazObszar(Rectangle rectangle) {
		this.rectangle = rectangle;
		
	  		EventQueue.invokeLater(new Runnable() {
	            @Override
	            public void run() {
	                try {
	                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
	                }

	                frame = new JDialog();
	                frame.setUndecorated(true);
	                frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	                // This works differently under Java 6
	                frame.setBackground(new Color(0, 0, 0, 0));
	                //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	                frame.setLayout(new BorderLayout());
	                frame.add(new SnipItPane());
	                frame.setBounds(getVirtualBox(1));
	                
	                frame.setModal(true);
	                
	                frame.setVisible(true);
	            }
	        });
	}*/
	public void Obszar(){
		
		JDialog frame = new JDialog();

        frame.setUndecorated(true);
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        // This works differently under Java 6
        frame.setBackground(new Color(0, 0, 0, 0));
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new SnipItPane());
        frame.setBounds(getVirtualBox(1));
        
        frame.setModal(true);
        
        frame.setVisible(true);
	}
	
	
	
	public class SnipItPane extends JPanel implements snipItpane_interf{
		Point mouseAnchor;
		Point dragPoint;
		
		SelectionPane selectionPane;
						
		public SnipItPane(){
			setOpaque(false);
			setLayout(null);
			
			
			infopanel = new InfoPanel();
			add(infopanel);
			
			selectionPane = new SelectionPane();
			add(selectionPane);
					
			MouseAdapter ma = new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					mouseAnchor = e.getPoint();
					dragPoint = null;
					selectionPane.setLocation(mouseAnchor);
					selectionPane.setSize(0,0);

				}
				
				@Override
				public void mouseDragged(MouseEvent e) {
					dragPoint = e.getPoint();
					int width = dragPoint.x - mouseAnchor.x;
					int height = dragPoint.y - mouseAnchor.y;
										
					int x = mouseAnchor.x;
					int y = mouseAnchor.y;
					
		               if (width < 0) {
		            	    x= dragPoint.x;
	                        width *= -1;   
	                    }
		               
	                    if (height < 0) {
	                    	y = dragPoint.y;
	                        height *= -1;
	                    }
	                    
					selectionPane.setBounds(x, y, width, height);
					infopanel.setVisible(true);
					infopanel.setBoundsInfoPanel (x, y, width, height);
					revalidate();
					repaint();
				}
				
				@Override
				public void mouseMoved(MouseEvent e) {
					super.mouseMoved(e);
				}
			};
			addMouseListener(ma);
			addMouseMotionListener(ma);
			
			KeyAdapter ka = new KeyAdapter() {
		
				@Override
				public void keyPressed(KeyEvent e) {
					super.keyPressed(e);
					if (e.getKeyCode() == KeyEvent.VK_ESCAPE) CloseWindow();
				}
			};
			addKeyListener(ka);
            setFocusable(true);
            requestFocusInWindow();
		}
		
		protected void CloseWindow() {
			rectangle=new Rectangle (selectionPane.getX(),selectionPane.getY(),
					selectionPane.getWidth(),  selectionPane.getHeight());
			SwingUtilities.getWindowAncestor(SnipItPane.this).dispose();
		}

		@Override
		public void paintComponent(Graphics g) {
			Graphics2D g2d = (Graphics2D) g.create();
			
			Rectangle bounds = new Rectangle(0, 0, getWidth(), getHeight());
			Area area = new Area(bounds);
			area.subtract(new Area(selectionPane.getBounds()));
			
            g2d.setColor(new Color(255, 255, 255, 20));
            g2d.fill(area);
		}
	}
	
	
	public class SelectionPane extends JPanel {
		JButton btnCancel;
		JButton btnZapisz;
		JButton btnRezygnuje;
		JLabel label;
		
		public SelectionPane(){
			btnCancel = new JButton("Close");
			setOpaque(true);
			btnCancel.setSize(65, 35);
			setBackground(new Color(220, 230, 250, 70));
						
			label = new JLabel("Rectangle");
			label.setOpaque(true);
			label.setBorder(new EmptyBorder(4, 4, 4, 4));
            label.setBackground(new Color(20, 20, 20, 20) );
            label.setForeground(Color.BLACK);
            setLayout(new GridBagLayout());
            
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridx = 0;
			gbc.gridy = 0;
			add(label, gbc);
			
			gbc.gridy ++;
			add(btnCancel, gbc);
	
			
		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				infopanel.setVisible(false);
				rectangle.setBounds(getX(), getY(), getWidth(),  getHeight());				
				SwingUtilities.getWindowAncestor(SelectionPane.this).dispose();

			}
		});
		
        addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent e) {
            	
                label.setText("Rectangle " + getX() + "x" + getY() + "x" + getWidth() + "x" + getHeight());
                if (!getBounds().equals(new Rectangle(0, 0, 0, 0))) infopanel.setVisible(true);
                else infopanel.setVisible(false);
            }
        });
        
				
		}
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2D = (Graphics2D) g.create();
			
            float dash1[] = {5.0f};
            BasicStroke dashed =
                    new BasicStroke(0.5f,
                    BasicStroke.CAP_BUTT,
                    BasicStroke.JOIN_MITER,
                    10.0f, dash1, 0.0f);
            g2D.setColor(Color.GRAY);
            g2D.setStroke(dashed);
            g2D.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
            g2D.dispose();
		}
	}
	/**
	 * 
	 * @param current
	 * @param 1 określa size danego monitora
	 * @param 2 określa multimonitor
	 * @return
	 */
	public static Rectangle getVirtualBox(int current) {
		
		Rectangle bounds = new Rectangle(0, 0, 0, 0);
		Dimension demScreen = Toolkit.getDefaultToolkit().getScreenSize();
		
		if (current == 1){
			bounds.add(new Rectangle (0,0,demScreen.width, demScreen.height));
		}
		
		if (current == 2){
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			GraphicsDevice gds[] = ge.getScreenDevices();
			for (GraphicsDevice gD : gds){
				//System.out.println("gds " + gds.length +" "+ gD.getFullScreenWindow().getWidth() );
				bounds.add(gD.getDefaultConfiguration().getBounds());
		
			}
		}
		return bounds;
	}

	@Override
	public void iUstawObszar(Rectangle obszar) {
		this.rectangle = obszar;
	}

}
